package com.iman.manjusri.service.impl;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.iman.common.hibernate.HibernateSupport;
import com.iman.common.hibernate.TemplateQLService;
import com.iman.common.util.DateUtils;
import com.iman.common.util.IdCreater;
import com.iman.manjusri.mappers.DrmBuyCartMapper;
import com.iman.manjusri.mappers.DrmStudyCoinMapper;
import com.iman.manjusri.mappers.models.DrmBuyCart;
import com.iman.manjusri.mappers.models.DrmBuyCartExample;
import com.iman.manjusri.mappers.models.DrmStudyCoin;
import com.iman.manjusri.mappers.models.DrmStudyCoinExample;
import com.iman.manjusri.service.BuyCartService;
import com.iman.manjusri.vo.DrmCourseVo;

/*
 * 
 * *************************************************************************
 * @文件名称: BuyCartServiceImpl.java
 *
 * @包路径  : com.iman.manjusri.service.impl 
 *				 
 * @版权所有:   TSW   科技有限公司 (C) 2014
 *
 * @类描述:  
 * 
 * @创建人:   wangxiaolei  
 *
 * @创建时间: 2015年3月15日 - 下午7:29:10 
 *
 * @修改记录:
   -----------------------------------------------------------------------------------------------
             时间						|		修改人		|		修改的方法		|		修改描述                                                                
   -----------------------------------------------------------------------------------------------
							|					|					|                                       
   ----------------------------------------------------------------------------------------------- 	
 
 **************************************************************************
 */
@Service
public class BuyCartServiceImpl implements BuyCartService{

	@Inject
	DrmBuyCartMapper  drmBuyCartMapper;
    @Inject
    TemplateQLService templateQLService;
 
    @Inject
    HibernateSupport  hibernateSupport;

	@Inject
	DrmStudyCoinMapper  drmStudyCoinMapper;
    
	@Override
	public boolean deleteByUserIdCourseId(Long userId, Long courseId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteByPk(Long cartId, Long courseId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteByUserId(Long userId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteByCartId(Long cartId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("cartId", cartId);
		int count = templateQLService.executeSQLUpdate("update_cart_delete", param);
		return count > 0;
	}

	@Override
	public boolean deleteByCartIdCourseId(Long cartId,Long courseId){
		DrmBuyCartExample drmbuy = new DrmBuyCartExample();
		drmbuy.createCriteria().andCartIdEqualTo(cartId).andCourseIdEqualTo(courseId);
		int result = this.drmBuyCartMapper.deleteByExample( drmbuy);
		return  result>0?true:false;
	};

	/**
	 * 过滤购物车中重复的课程添加
	 */
	@Override
	public Map<String,DrmCourseVo> getCartCourseIds(Long userId) {
		DrmBuyCartExample dce = new DrmBuyCartExample();
		dce.createCriteria().andUserIdEqualTo(userId).andPutTimeGreaterThanOrEqualTo(DateUtils.getLastDate(30))
				.andCartStEqualTo(1);

		List<DrmBuyCart> drmbuycart = this.drmBuyCartMapper.selectByExample(dce);
		Map<String,DrmCourseVo> result = new HashMap<String,DrmCourseVo>();

		if (drmbuycart != null) {
			for (DrmBuyCart d : drmbuycart) {
				DrmCourseVo vo = new DrmCourseVo();
				vo.cartId = d.getCartId() ;
				vo.setCourseId( d.getCourseId());
				result.put(d.getCartId().toString() + d.getCourseId().toString() ,vo ) ;
			}
		}
		return result;
	}

	public List<DrmCourseVo> getCartWithCourse(Long userId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("userId", userId);
		return templateQLService.findBySQLBeanQuery("list_cart_with_course", param, DrmCourseVo.class);
	}

	@Override
	public List<DrmCourseVo> getCartWithCourse(Long cartId[]) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("cartId", Arrays.asList(cartId));
		return templateQLService.findBySQLBeanQuery("list_cart_with_course", param, DrmCourseVo.class);
	}

	/**
	 * 
	 */
	public int getCartWithCourseCount(Long userId){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("userId", userId);
		return ( (BigInteger)templateQLService.findSQL("list_cart_with_course_count", param )).intValue();
	};

	/**
	 * 修改返回参数：当新增成功，返回购物车id，否则返回-1；
	 */
	@Override
	public long addCourseToCart(Long userId, Long courseId) {
		return addCourseToCart(userId, courseId, 0);
	}

	/**
	 * 修改返回参数：当新增成功，返回购物车id，否则返回-1；
	 */
	@Override
	public long addCourseToCart(Long userId, Long courseId, Integer cartTp) {
		DrmBuyCart cart = new DrmBuyCart();
		cart.setCancelDt(null);
		cart.setCartId(IdCreater.getId());
		cart.setCartSt(1);
		cart.setCourseId(courseId);
		cart.setPutTime(new Date());
		cart.setUserId(userId);
		cart.setCartTp(1);
		int result = this.drmBuyCartMapper.insertSelective(cart);
		return result > 0 ? cart.getCartId() : -1;
	}

	/**
	 * 反过来多少学习币    抵   1元                          这算，一人民币，值多少学习比，
	 * 
	 * 
	 */
	@Override
	public int get1YuanRMBTransferStudyCoin() {
		DrmStudyCoinExample dbce = new DrmStudyCoinExample();
		
		dbce.createCriteria().andUseYnEqualTo( 1);
		List<DrmStudyCoin> result =   this.drmStudyCoinMapper.selectByExample(dbce );
		
		try{
			if( result != null && result.size() >0 ){
				DrmStudyCoin coin = result.get(0);
				int coins = coin.getScoinCnt() .intValue() / coin.getRmbCnt().intValue();
				return coins;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return 0;
	}

}
